package org.compiere.process;

import java.util.List;

import org.compiere.model.MBSCCard;
import org.compiere.model.MParameter;
import org.compiere.model.MPeriod;
import org.compiere.model.Query;
import org.compiere.util.CLogger;
import org.compiere.util.Env;

public class BSCCheckingDistributionParameters extends SvrProcess {

	protected static CLogger log = CLogger.getCLogger(BSCCheckingDistributionParameters.class);
	
	private List<MBSCCard> cards;
	private List<MParameter> parameters;
	private MPeriod period;
	private int AD_Client_ID = 0 ; // Env.getAD_Client_ID(getCtx());
	private int AD_Org_ID = 0; // Env.getAD_Org_ID(getCtx());
	private String resultIt = null;
	private int C_Period_ID = 0;
	private MBSCCard bossCard;

	@Override
	protected void prepare() {
		AD_Client_ID = Env.getAD_Client_ID(getCtx());
		AD_Org_ID = Env.getAD_Org_ID(getCtx());
		for(ProcessInfoParameter param: getParameter()) {
			if (param.getParameterName().equals("C_Period_ID")) {
				setC_Period_ID(Integer.parseInt(param.getParameter().toString()));
			}
		}
		// TODO Auto-generated method stub
		
	}

	@Override
	protected String doIt() throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	public List<MBSCCard> getCards() {
		return cards;
	}

	public void setCards(List<MBSCCard> cards) {
		this.cards = cards;
	}

	public List<MParameter> getParameters() {
		return parameters;
	}

	public void setParameters(List<MParameter> parameters) {
		this.parameters = parameters;
	}

	public MPeriod getPeriod() {
		if (period == null && getC_Period_ID() > 0) {
			period = MPeriod.get(getCtx(),getC_Period_ID());;
		}		
		return period;
	}

	public void setPeriod(MPeriod period) {
		this.period = period;
	}

	public int getC_Period_ID() {
		return C_Period_ID;
	}

	public void setC_Period_ID(int c_Period_ID) {
		C_Period_ID = c_Period_ID;
		MPeriod p = MPeriod.get(getCtx(),C_Period_ID);
		if (p != null) {
			setPeriod(p);
		}
	}
	
	public MBSCCard getBossCard() {
		if (bossCard == null && period != null) {
			String WhereClause = "C_Period_ID = ? AND C_BPartner_ID = (SELECT C_BPartner_ID FROM BSC_Dashboard WHERE numberofruns = (SELECT MIN(numberofruns) FROM BSC_Dashboard))";
			List<MBSCCard> list = new Query(Env.getCtx(),MBSCCard.Table_Name,WhereClause,get_TrxName())
			                         .setParameters(new Integer(getC_Period_ID()))
			                         .list();
			if (list != null && list.size() > 0) {
				bossCard = list.get(0);
			}
		}
		return bossCard;
	}
}
